package com.bawei.module_mine.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bawei.module_mine.databinding.LayoutSettingItemBinding
import com.bawei.module_mine.model.SettingEntity
import com.bumptech.glide.Glide

class SettingAdapter(val data:List<SettingEntity>):RecyclerView.Adapter<SettingAdapter.SettingViewHolder>() {

    private var onItemClickListener: ((position: Int) -> Unit)? = null
    
    fun setOnItemClickListener(listener: (position: Int) -> Unit) {
        onItemClickListener = listener
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SettingViewHolder {
         val itemDataBinding=
             LayoutSettingItemBinding.inflate(LayoutInflater.from(parent.context),parent,false)
        return SettingViewHolder(itemDataBinding)
    }

    override fun getItemCount(): Int {
        return data.size
    }

    override fun onBindViewHolder(holder: SettingViewHolder, position: Int) {
        val itemData=data[position]
        
        // 使用DataBinding来设置数据
        holder.itemDataBinding.apply {
            // 设置图标
            Glide.with(ivIcon.context).load(itemData.img).into(ivIcon)
            // 设置标题
            tvTitle.text = itemData.title
            
            // 设置点击事件
            root.setOnClickListener {
                onItemClickListener?.invoke(position)
            }
        }
    }

    class SettingViewHolder(val itemDataBinding:LayoutSettingItemBinding):RecyclerView.ViewHolder(itemDataBinding.root)

}